﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using Clandestine.Conversations;
using JibberJabber.PartDesigners;

namespace JibberJabber
{
    public static class Program
    {
        public static bool SavedEver = false;
        public static string Filename = "Untitled";

        public static RootNodeDesigner Root;
        public static SpeakersNodeDesigner Speakers
        {
            get
            {
                lock (Root.Children)
                    return ((SpeakersNodeDesigner)Root.Children[0]);
            }
        }
        public static Preview Preview;
        public static frmMain frmMain;

        public static SpeakerDesigner FindSpeakerByName(string name)
        {
            lock (Speakers.Children)
                foreach (PartDesigner pd in Speakers.Children)
                    if (((SpeakerDesigner)pd).txtName.Text == name)
                        return ((SpeakerDesigner)pd);

            // Otherwise...
            return null;
        }

        public static void NotifyNodesUpdated()
        {
            frmMain.UpdateTree();
        }

        public static void NotifyNodeToStringOutputUpdated(PartDesigner pd)
        {
            if (pd.AssociatedNode != null)
            {
                pd.AssociatedNode.Text = pd.ToString(); // Simples!
                pd.callToStringOutputUpdatedProgramDotCsHook();
            }
        }

        public static void AddChildPartDesigner(PartDesigner parent, PartDesigner child)
        {
            lock (parent.Children)
                parent.Children.Add(child);
            parent.NotifyChildrenUpdated();
            Program.NotifyNodesUpdated();
        }

        public static void RemoveChildPartDesigner(PartDesigner parent, PartDesigner child)
        {
            lock (parent.Children)
                parent.Children.Remove(child);
            parent.NotifyChildrenUpdated();
            Program.NotifyNodesUpdated();
        }

        public static void Reset()
        {
            Program.Preview.Pause();
            Program.Preview.ClearFuckingEverything();
            Program.Preview.Resume();

            frmMain.DeselectCurrPd();

            Program.Root = new RootNodeDesigner();
            Program.NotifyNodesUpdated();

            SavedEver = false;
            Filename = "Untitled";
        }

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Clandestine.Program.WorkingDirectoryFixUp();
            Preview = new Preview();

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            Root = new RootNodeDesigner();
            frmMain = new frmMain();
            NotifyNodesUpdated();
            Application.Run(frmMain);
        }
    }
}
